package com.thrd.taiexhibition.activity;

import android.os.Bundle;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.thrd.taiexhibition.R;
import com.thrd.taiexhibition.base.BackTitleActivity;
import com.thrd.taiexhibition.bean.BeanBusiness;
import com.thrd.taiexhibition.bean.BeanCompany;
import com.thrd.taiexhibition.bean.BeanCooperation;
import com.thrd.taiexhibition.bean.BeanEnterprise;
import com.thrd.taiexhibition.bean.BeanInterview;
import com.thrd.taiexhibition.fragement.enterprise.BusinessFragment;
import com.thrd.taiexhibition.fragement.enterprise.InterviewFragment;
import com.thrd.taiexhibition.fragement.enterprise.LiabilityFragment;
import com.thrd.taiexhibition.fragement.enterprise.SocietyFragment;
import com.thrd.taiexhibition.fragement.enterprise.TopFragment;
import com.thrd.taiexhibition.presener.EnterpriseHomePresener;
import com.thrd.taiexhibition.ui.LinearEnterpriseView;
import com.thrd.taiexhibition.utils.DialogUtils;
import com.thrd.taiexhibition.view.EnterpriseHomeView;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * @author: ${唐杰}
 * @date: 2021/3/30
 */
@Route(path = "/app/enterprise",group = "app")
public class EnterpriseHomeActivity extends BackTitleActivity implements EnterpriseHomeView {

    private LinearEnterpriseView mLinearEnterpriseView;
    private EnterpriseHomePresener presener;

    @Override
    protected int getLayout() {
        return R.layout.activity_enterprise_home;
    }

    @Override
    protected void initView() {
        mLinearEnterpriseView = (LinearEnterpriseView)findViewById(R.id.lev_linearview);




    }

    @Override
    protected void initData() {
        EnterpriseHomePresener presener= new EnterpriseHomePresener(this);
        presener.getCityStorData(1);



        List<String> list=new ArrayList<>();
        list.add("TOP");
        list.add("商业发布");
        list.add("社会合作");
        list.add("企业采访");
        list.add("我要参展");
        list.add("商业调研");
        mLinearEnterpriseView.setStringList(list);


        //switchItem(0);
    }

    private BusinessFragment mBusinessFragment;
    private TopFragment mTopFragment;
    private SocietyFragment mSocietyFragment;
    private InterviewFragment mInterviewFragment;
    private LiabilityFragment mLiabilityFragment;
    @Override
    protected void initEvent() {
        mLinearEnterpriseView.setOnItemClickListene(new LinearEnterpriseView.OnItemClickListene() {
            @Override
            public void onItemClickListene(int item) {
                position=item;
                if(mBeanEnterprise!=null){
                    switchItem(item);
                }

            }
        });
    }

    private int position;
    private void switchItem(int item){
        Bundle bundle=null;
        switch (item){
            case 0:
                if(mTopFragment==null) {
                    mTopFragment = new TopFragment();
                    BeanCompany mBeanCompany=mBeanEnterprise.getCompany();
                    bundle=new Bundle();
                    bundle.putSerializable("BeanCompany",  mBeanCompany);
                    mTopFragment.setArguments(bundle);
                }
                switchFragment(mTopFragment);
                break;

            case 1:
                if(mBusinessFragment==null){
                    mBusinessFragment= new BusinessFragment();
                    List<BeanBusiness> mBeanBusinessList=mBeanEnterprise.getThComProductList();
                    bundle=new Bundle();
                    bundle.putSerializable("BeanBusinessList", (Serializable) mBeanBusinessList);
                    mBusinessFragment.setArguments(bundle);
                }
                switchFragment(mBusinessFragment);
                break;

            case 2:
                if(mSocietyFragment==null){
                    mSocietyFragment=new SocietyFragment();
                    List<BeanCooperation> mBeanCooperationList=mBeanEnterprise.getCompanyExtList();
                    bundle=new Bundle();
                    bundle.putSerializable("BeanCooperationList", (Serializable) mBeanCooperationList);
                    mSocietyFragment.setArguments(bundle);
                }
                switchFragment(mSocietyFragment);
                break;

            case 3:
                if(mInterviewFragment==null){
                    mInterviewFragment=new InterviewFragment();
                    List<BeanInterview> mBeanBeanInterviewList=mBeanEnterprise.getInterviewList();
                    bundle=new Bundle();
                    bundle.putSerializable("BeanInterviewList", (Serializable) mBeanBeanInterviewList);
                    mInterviewFragment.setArguments(bundle);
                }
                switchFragment(mInterviewFragment);
                break;

            case 4:
                DialogUtils.showQRDialog(EnterpriseHomeActivity.this,null,"1");
                break;
            case 5:
                DialogUtils.showQRDialog(EnterpriseHomeActivity.this,null,"2");
                break;

        }

    }

    private Fragment currentFragment;
    /**
     *
     * @param targetFragment
     * @return
     */
    private FragmentTransaction switchFragment(Fragment targetFragment) {

        FragmentTransaction transaction = getSupportFragmentManager()
                .beginTransaction();
        if (!targetFragment.isAdded()) {
            //第一次使用switchFragment()时currentFragment为null，所以要判断一下
            if (currentFragment != null) {
                transaction.hide(currentFragment);
            }
            transaction.add(R.id.fl_enterprise_layout, targetFragment,targetFragment.getClass().getName());

        } else {
            transaction
                    .hide(currentFragment)
                    .show(targetFragment);
        }
        currentFragment = targetFragment;
        transaction.commit();
        return   transaction;
    }

    @Override
    public void onError(String error, int type) {

    }
    private BeanEnterprise mBeanEnterprise;
    @Override
    public void onSuccess(BeanEnterprise mBeanEnterprise) {
        this.mBeanEnterprise=mBeanEnterprise;
        switchItem(position);
    }
}
